package cn.dglydrpy.study.algorithm.od;

import java.util.Scanner;

/**
 * @author Yyy
 * @date 2024/11/5 12:20
 * @description 
 * 题目描述：给定一个包含 0 和 1 的二维矩阵，一个物体从给定初始位置和速度出发，遇到矩阵边缘则发生镜面反射，
 * 计算经过 t 时间单位后，物体经过 1 点的次数。
 */
public class Od0003_ReflectCount {

    public static void main(String[] args) {
//        int w = 12;
//        int h = 7;
//        int x = 2;
//        int y = 1;
//        int sx = 1;
//        int sy = -1;
//        int t = 13;
//        int[][] board = new int[][]{
//                new int[]{0,0,1,0,0,0,0,1,0,0,0,0},
//                new int[]{0,0,1,0,0,0,0,1,0,0,0,0},
//                new int[]{0,0,1,0,0,0,0,1,0,0,0,0},
//                new int[]{0,0,1,0,0,0,0,1,0,0,0,0},
//                new int[]{0,0,1,0,0,0,0,1,0,0,0,0},
//                new int[]{0,0,1,0,0,0,0,1,0,0,0,0},
//                new int[]{0,0,1,0,0,0,0,1,0,0,0,0}
//        };

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String[] s = line.split(" ");
            int w = Integer.valueOf(s[0]);
            int h = Integer.valueOf(s[1]);
            int x = Integer.valueOf(s[2]);
            int y = Integer.valueOf(s[3]);
            int sx = Integer.valueOf(s[4]);
            int sy = Integer.valueOf(s[5]);
            int t = Integer.valueOf(s[6]);
            int[][] board = new int[h][w];
            for (int i = 0; i < h; i++) {
                String s1 = scanner.nextLine();
                char[] chars = s1.toCharArray();
                for (int j = 0; j < w; j++) {
                    board[i][j] = chars[j] - '0';
                }
            }
            System.out.println(myAnswer(w,h,x,y,sx,sy,t,board));
            System.out.println(answer1(w,h,x,y,sx,sy,t,board));
            System.out.println(answer2(w,h,x,y,sx,sy,t,board));
            
        }
       
    }
    
    private static int myAnswer(int w,int h,int x,int y,int sx,int sy,int t,int[][] board){
        
        int ans = 0;
        
        // 出发点
        if(board[y][x] == 1){
            ans = 1;
        }
        
        // 开始运动
        while(t > 0){
            
            // 判断速度
            // sx和sy碰到边界会改变值
            if(y == 0 || y == h-1){
                sy = -sy;
            }else if(x == 0 || x == w-1){
                sx = -sx;
            }
            System.out.println("当前速度：（" + sx + "," + sy +")");
            
            x = x + sx;
            y = y + sy;
            System.out.println("当前位置：（" + x + "," + y +")");
            if(board[y][x] == 1){
                ans ++;
            }
            
            t--;
            System.out.println("剩余移动时间：" + t);
            
        }

        return ans;
    }

    

    private static int answer1(int w,int h,int x,int y,int sx,int sy,int t,int[][] board) {
            int count = 0;
            while (t >= 0) {
                if (board[y][x] == 1) {
                    count++;
                }
                if (y + sy < 0 || (y + sy > h - 1)) {
                    sy = -sy;
                }
                if (x + sx < 0 || (x + sx > w - 1)) {
                    sx = -sx;
                }
                x += sx;
                y += sy;
                t--;
            }
            return count;
        }

    public static int answer2(int w,int h,int x,int y,int sx,int sy,int t,int[][] board) {
        return move(board, x, y, sx, sy, t);
    }

    private static int move(int[][] map, int x, int y, int sx, int sy, int time) {
        int count = 0;
        if (time == 0) {
            if (map[y][x] == 1) {
                count++;
            }
            return count;
        }
        if (map[y][x] == 1) {
            count++;
        }
        if (x == 0 && sx < 0 || x == map[0].length - 1 && sx > 0) {
            sx = -sx;
        }
        if (y == 0 && sy < 0 || y == map.length - 1 && sy > 0) {
            sy = -sy;
        }
        return count + move(map, x + sx, y + sy, sx, sy, time - 1);
    }
}
